package cn.xuewenbao.ucenter.controller;


import cn.xuewenbao.ucenter.entity.Message;
import cn.xuewenbao.ucenter.entity.vo.MemberMesVo;

import cn.xuewenbao.ucenter.service.MessageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mafayu
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/ucenter/message")
@Api(tags = "私信表")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    //获得私信列表页面
    @ApiOperation(value = "获得私信列表页面")
    @GetMapping("getMemberList/{userId}")
    public Result<List<MemberMesVo>> getMemberList(@PathVariable String userId) {
        Result<List<MemberMesVo>> result = messageService.getMemberList(userId);
        return result;
    }

    //获取两个用户的私信界面
    @ApiOperation(value = "获取两个用户的私信界面")
    @GetMapping("getMessageList/{sendId}/{receiveId}/{pageSize}/{beforeTime}")
    public Result<?> list(@PathVariable String sendId,
                          @PathVariable String receiveId,
                          @PathVariable Integer pageSize,
                          @PathVariable Date beforeTime) {
        if (sendId == null || receiveId == null || beforeTime == null || pageSize == null || pageSize <= 0) {
            return Result.error("getMessageList接口参数错误");
        }
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.eq("send_id", sendId).eq("receive_id", receiveId).or().eq("receive_id", sendId).eq("send_id", receiveId));
        queryWrapper.lt("gmt_create", beforeTime);
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.last("limit " + pageSize);
        //查出 beforeTime时间之前的 pageSize条私信
        List<Message> messageList = messageService.list(queryWrapper);

        //判断是否有时间更早的私信记录
        queryWrapper.last("");    //覆盖 limit条件
        int count = messageService.count(queryWrapper); //满足查询条件的总私信数
        boolean hasMore = (count != messageList.size());

        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", messageList);
        map.put("rowsSize", messageList.size());
        map.put("total", count);
        map.put("hasMore", hasMore);
        return Result.OK(map);
    }

    //格式化 URL传入的时间(String -> Date), 只在当前 controller生效
    @InitBinder
    public void initDateFormate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
    }

//    @ApiOperation(value = "获取两个用户的私信界面")
//    @GetMapping("getMessageList/{sendId}/{receiveId}/{pageNo}/{pageSize}")
//    public Result<?> list(@PathVariable Integer pageNo,
//                          @PathVariable Integer pageSize,
//                          @PathVariable String sendId,
//                          @PathVariable String receiveId) {
//        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("send_id", sendId).eq("receive_id", receiveId);
//        queryWrapper.or().eq("receive_id", sendId).eq("send_id", receiveId);
//        queryWrapper.orderByAsc("gmt_create");
//
//        Page<Message> page = new Page<>(pageNo, pageSize);
//
//        IPage<Message> pageList = messageService.page(page, queryWrapper);
//
//        log.info("查询当前页：" + pageList.getCurrent());
//        log.info("查询当前页数量：" + pageList.getSize());
//        log.info("查询结果数量：" + pageList.getRecords().size());
//        log.info("数据总数：" + pageList.getTotal());
//        return Result.OK(pageList);
//    }

}

